package com.taru.order.controller;

import com.taru.order.common.JsonResult;
import com.taru.order.entity.Articl;
import com.taru.order.entity.Comment;
import com.taru.order.service.CommentService;
import com.taru.order.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CommentController {
    /**
     * 评论文章
     */
    @RequestMapping(value="/comarticl",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult commentArticl(@RequestBody Comment comment){
        JsonResult result=null;
        CommentService commentService=new CommentService();
        try {
            int isn = commentService.commentArticl(comment);
            if(isn!=1){
                result=new JsonResult(404,"评论失败");
            }
            else{
                result=new JsonResult(200,"评论成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 回复评论
     */
        @RequestMapping(value="/replaycomment",method = RequestMethod.POST)
        @ResponseBody
        public JsonResult replayComment(@RequestBody Comment comment){
            JsonResult result=null;
            CommentService commentService=new CommentService();
            try {
                int isn = commentService.replayComment(comment);
                if(isn!=1){
                    result=new JsonResult(404,"回复失败");
                }
                else{
                    result=new JsonResult(200,"回复成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
                result=new JsonResult(500,"服务器内部错误");
            }
            return result;
        }
    /**
     * 查询文章的评论
     */
    @RequestMapping(value="/selectcomment",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectComment(String articl_id){
        JsonResult result=null;
        CommentService commentService=new CommentService();
        List<Comment> comments=null;
        try {
           comments=commentService.selectComment(articl_id);
            if(comments==null){
                result=new JsonResult(404,"查询失败");
            }
            else{
                result=new JsonResult(200,"查询成功",comments);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 通过comment_id查询评论的回复
     */
    /*@RequestMapping(value="/selectreplay",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectReplay(String comment_id){
        JsonResult result=null;
        CommentService commentService=new CommentService();
        List<Comment> comments=null;
        try {
            comments=commentService.selectReplay(comment_id);
            if(comments==null){
                result=new JsonResult(404,"查询失败");
            }
            else{
                result=new JsonResult(200,"查询成功",comments);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }*/
}
